package edu.chalmers.scv;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;

import android.nfc.NfcEvent;
import edu.chalmers.targine.ARManager;
import edu.chalmers.targine.Arbject;
import edu.chalmers.targine.GameLayer;
import edu.dhbw.andar.exceptions.AndARException;
import edu.dhbw.andar.util.AssetsFileUtil;
import edu.dhbw.andar.util.BaseFileUtil;

public class Scavengers extends ARManager{
//	private ScavengerArbject currentArbject;
	private Text scoreText, trapText;
	private Button trapButton;
	private TreasureIcon ownIcon, badIcon;
	
	private int score = 0;
	private int traps = 2;
	
	public static final String TREASURE = "treasure";
	public static final String PICK = "pick";
	public static final String TRAP = "trap";
	
	public static final String UNICORN = "unicorn";
	public static final String CHEST = "chest";
	public static final String CUP = "cup";
	
	public static final String NORMAL = "normal";
	public static final String TRAPPING = "trapping";
	private String gameMode = NORMAL;
	
	private String ownTreasure = UNICORN;
	private String badTreasure = CUP;
	
	@Override
    public void onStart(){
    	super.onStart();
    	//counters
    	scoreText = new Text(98, 225, ""+score);
    	((GameLayer)(cocosScene.getChildren().get(0))).addComponent(scoreText);
    	trapText = new Text(WINDOW_WIDTH - 175, 500, ""+traps);
    	//trap button
    	((GameLayer)(cocosScene.getChildren().get(0))).addComponent(trapText);
    	trapButton = new Button("button_trap.png", WINDOW_WIDTH - 175, 600, TRAP);
    	((GameLayer)(cocosScene.getChildren().get(0))).addComponent(trapButton);
    	//treasure icons
    	ownIcon = new TreasureIcon("icons_treasure.png", 175, 50);
	    ((GameLayer)(cocosScene.getChildren().get(0))).addComponent(ownIcon);
	    badIcon = new TreasureIcon("icons_treasure.png", WINDOW_WIDTH - 50, 50);
	    ((GameLayer)(cocosScene.getChildren().get(0))).addComponent(badIcon);
//    	currentArbject = null;
	    ArrayList<String> allLoot = new ArrayList<String>();
	    ArrayDeque<String> pollLoot = new ArrayDeque<String>();
	    for(int i = 0; i < 4; i++){
	    	allLoot.add(UNICORN);
	    	allLoot.add(CHEST);
	    	allLoot.add(CUP);
	    }
	    Collections.shuffle(allLoot);
	    pollLoot.addAll(allLoot);
    	for(Arbject arbject : arbjects.values()){
    		if(!pollLoot.isEmpty()){
    			setTreasure((ScavengerArbject)arbject, pollLoot.pollFirst());
    		} else{
    			setTreasure((ScavengerArbject)arbject, CHEST);
    		}
    	}
    	//determine treasure value from phone id
    	if(LOCAL_MAC.equals("a0 0b ba e0 b4 0e")){
	    	ownTreasure = UNICORN;
	    	badTreasure = CHEST;
    	} else if(LOCAL_MAC.equals("a0 0b ba e0 69 00")){
	    	ownTreasure = CHEST;
	    	badTreasure = CUP;
    	} else{
    		ownTreasure = CUP;
	    	badTreasure = UNICORN;
    	}
    	//TODO: set value for all treasure cards
    }
	
	@Override
	protected void registerModels(){
		arbjects = new ConcurrentHashMap<String, Arbject>();
		
    	try {    		
    		BaseFileUtil fileUtil = new AssetsFileUtil(getResources().getAssets());
			fileUtil.setBaseFolder("models/");			

//			ObjParser parser = new ObjParser(fileUtil);
			if(fileUtil != null){
//				BufferedReader fileReader;
//				fileReader = fileUtil.getReaderFromName("ski_slope_1_new.obj");
//				Log.d("LOAD_MODEL", "Loading ski_slope_1");
//				models.put("ski_slope_1", parser.parse("ski_slope_1", fileReader));
				
				registerArbject("0c 10 ef 0f", "linda", "pattern1");
				registerArbject("ae e0 41 d2", "oleander", "pattern3");
				registerArbject("5e 6a 42 d2", "pokeylope", "pattern2");
				registerArbject("9e cc 40 d2", "sasha", "pattern4");
				registerArbject("ee 0a 6d b7", "shield", "pattern_shield");
				registerArbject("de cf 3d d2", "lens", "pattern_lens");
				registerArbject("ee 42 39 d2", "harry", "pattern_harry");
//				registerArbject("6c be 02 06 66 24 16 e0", "harry", "pattern_harry");
				registerArbject("ae 67 3f d2", "tetris", "pattern_tetris");
				registerArbject("ce 37 c6 aa", "axe", "pattern_axe");
				registerArbject("fe c3 c7 aa", "castle", "pattern_castle");
				registerArbject("6e b6 6e b7", "sword", "pattern_sword");
				registerArbject("ee aa 3d d2", "moon", "pattern_moon");
			}
		} catch (AndARException e){
			e.printStackTrace();
		}
//    	catch (IOException e) {
//			e.printStackTrace();
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
    }
	
	//TODO: Fix this so that it works with Scavengers instead of Game of Stones.
	@Override
	public void registerArbject(String arbjectID, String patternName, String patternFile) throws AndARException{
		arbjects.put(arbjectID, new ScavengerArbject(patternName, patternFile, arbjectID, LOCAL_MAC, this));
    	serverConnection.sendCommandToServer("currentValue:" + arbjectID + "." + TREASURE);
    	artoolkit.registerARObject(arbjects.get(arbjectID));
    }
	
	@Override
	public void update(){
		ownIcon.setImage(ownTreasure);
		badIcon.setImage(badTreasure);
	}
	
	public void serverUpdate(){
    }
	
	protected void onBeamComplete(NfcEvent event){
	}

	@Override
	protected void beamReceived(String deviceMac){
	}
	 
	@Override
	protected void nfcReceived(Arbject arbject){
		if(gameMode.equals(NORMAL)){
			getTreasure((ScavengerArbject)arbject);
//			currentArbject = (ScavengerArbject)arbject;
		} else if(gameMode.equals(TRAPPING)){
			addTrap((ScavengerArbject)arbject);
			gameMode = NORMAL;
			trapButton.setImage("button_trap.png");
		}
	}

	@Override
	public void performTapAction(String action, Object object){
		if(action.equals(TRAP)){
			if(!gameMode.equals(TRAPPING) && traps > 0){
				gameMode = TRAPPING;
				trapButton.setImage("button_trap2.png");
			} else{
				gameMode = NORMAL;
				trapButton.setImage("button_trap.png");
			}
		}
	}
	
	@Override
	protected void accelerometerChanged(float ax, float ay, float az){
    }
	
	
	@Override
	public void serverConnectionEventOccurred(String[] msg) {
		if(msg[1].equals(TREASURE)){
			((ScavengerArbject)arbjects.get(msg[0])).setTreasure(msg[2]);
		} else if(msg[1].equals(PICK)){
			((ScavengerArbject)arbjects.get(msg[0])).removeTreasure();
		} else if(msg[1].equals(TRAP)){
			((ScavengerArbject)arbjects.get(msg[0])).setTrap();
		}
	}
	
	private void setTreasure(ScavengerArbject arb, String treasure){
		setSharedData(arb.getID(), TREASURE, treasure);
	}
	
	private void getTreasure(ScavengerArbject arb){
		if(arb.hasTreasure()){
			score += arb.takeTreasure(ownTreasure, badTreasure);
			scoreText.setText(""+score);
		}
		setSharedData(arb.getID(), PICK, "");
	}
	
	private void addTrap(ScavengerArbject arb){
		traps --;
		trapText.setText(""+traps);
		setSharedData(arb.getID(), TRAP, "");
	}
}
